/*
 * @Author: your name
 * @Date: 2021-10-14 14:00:19
 * @LastEditTime: 2021-10-14 16:44:55
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \Alive-master\assets\script\game\player\movePlayer.ts
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class MovePlayer extends cc.Component {
	@property(cc.Node)
	player: cc.Node | undefined = undefined;

	onEnable() {
		this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
		this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
	}

	onDisable() {
		this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
		this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
	}

	onTouchStart(event: cc.Event.EventTouch) {
		let w_pos = event.getLocation();
		let pos = this.node.convertToNodeSpaceAR(w_pos);
		this.player.setPosition(pos);
	}

	onTouchMove(event: cc.Event.EventTouch): void {
		let delta = event.getDelta();
		this.player.x += delta.x;
		this.player.y += delta.y;
	}

	update(dt: number): void {
		// 边界限制
		if (this.player.x > 360) this.player.x = 360;
		if (this.player.x < -360) this.player.x = -360;
		if (this.player.y > 640) this.player.y = 640;
		if (this.player.y < -640) this.player.y = -640;
	}
}
